package service;

import common.Pagination;
import common.StringUtils;
import dao.CroCouponsDao;
import dao.CroMyqponDao;
import entity.CroCoupons;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.ShowCouponsVo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/7 0007.
 */
@Service
public class CroCouponsService {
    @Autowired
    private CroCouponsDao croCouponsDao;
    @Autowired
    private CroMyqponDao croMyqponDao;
    @Autowired
    CroCustomerService croCustomerService;

    /*
    * 查找所有优惠券
    * */
    @Transactional(readOnly = true)
    public List<CroCoupons> FindCoupons(){
        String hql="from CroCoupons ";
        List<CroCoupons> showCoupons = croCouponsDao.findList(hql);
        return showCoupons;
    }

    /*
   * 分页查找出全部优惠券
   * */
    @Transactional(readOnly = true)
    public Pagination<CroCoupons> findAllUser(int index, int size){
        String hql="from CroCoupons where del_flag='0'";
        Pagination<CroCoupons> croUserList=croCouponsDao.findPagination(hql,index,size);
        return croUserList;
    }


    /*
    * 分页查找出已发放优惠券
     * */
    @Transactional(readOnly = true)
    public Pagination<CroCoupons> findCoupons(int index, int size ){
        Date datetime = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String date= dateFormat.format( datetime );
        String hql="from CroCoupons where del_flag='0' and status='1' and offdate > '"+ date+"'";
        Pagination<CroCoupons> croUserList=croCouponsDao.findPagination(hql,index,size);
        return croUserList;
    }

    /*根据id查找单个优惠券*/
    @Transactional(readOnly = true)
    public CroCoupons FindCouponsByid(String id){
        CroCoupons croCoupons = croCouponsDao.getById(id);
        return croCoupons;
    }

    /*删除优惠券*/
    @Transactional(readOnly = false)
    public boolean deleteCoupons(String id){
        try {
            CroCoupons croCoupons = croCouponsDao.getById(id);
            croCoupons.setDel_flag("1");
            croCouponsDao.update(croCoupons);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 根据属性来查找优惠券
    * */
    @Transactional(readOnly = true)
    public Pagination<CroCoupons> FindUserByProperty(ShowCouponsVo showCouponsVo, int index, int size){

        String hql="from CroCoupons where del_flag='0' ";
        StringBuffer sb=new StringBuffer();
        sb.append(hql);
        if(StringUtils.isNotBlank(showCouponsVo.getDescription())){
            sb.append(" and description like '%"+showCouponsVo.getDescription()+"%'");
        }
        if(StringUtils.isNotBlank(showCouponsVo.getType())){
            sb.append(" and type='"+showCouponsVo.getType()+"'");
        }
        if (StringUtils.isNotBlank(showCouponsVo.getStartdate()) && StringUtils.isNotBlank(showCouponsVo.getEnddate())){
            sb.append(" and DATE_FORMAT(offdate,'%Y-%m-%d') between '"+showCouponsVo.getStartdate()+"' and '"+showCouponsVo.getEnddate()+"'");
        }else if (StringUtils.isNotBlank(showCouponsVo.getStartdate()) && !StringUtils.isNotBlank(showCouponsVo.getEnddate())){
            sb.append(" and DATE_FORMAT(offdate,'%Y-%m-%d') >='"+showCouponsVo.getStartdate()+"'");
        }else if(!StringUtils.isNotBlank(showCouponsVo.getStartdate()) && StringUtils.isNotBlank(showCouponsVo.getEnddate())){
            sb.append(" and DATE_FORMAT(offdate,'%Y-%m-%d') <='"+showCouponsVo.getEnddate()+"'");
        }

        Pagination<CroCoupons> croCoupons=croCouponsDao.findPagination(sb.toString(),index,size);
        return croCoupons;
    }

    /*
        * 根据属性来查找优惠券用户版
        * */
    @Transactional(readOnly = true)
    public Pagination<CroCoupons> FindCouponsByProperty(ShowCouponsVo showCouponsVo, int index, int size){
        Date datetime = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String date= dateFormat.format( datetime );
        String hql="from CroCoupons where del_flag='0' and status='1' and offdate > '"+ date+"'";
        StringBuffer sb=new StringBuffer();
        sb.append(hql);
        if(StringUtils.isNotBlank(showCouponsVo.getDescription())){
            sb.append(" and description like '%"+showCouponsVo.getDescription()+"%'");
        }
        if(StringUtils.isNotBlank(showCouponsVo.getType())){
            sb.append(" and type='"+showCouponsVo.getType()+"'");
        }
        if(StringUtils.isNotBlank(showCouponsVo.getStatus())){
            sb.append(" and status='"+showCouponsVo.getStatus()+"'");
        }

        if (StringUtils.isNotBlank(showCouponsVo.getStartdate()) && StringUtils.isNotBlank(showCouponsVo.getEnddate())){
            sb.append(" and DATE_FORMAT(offdate,'%Y-%m-%d') between '"+showCouponsVo.getStartdate()+"' and '"+showCouponsVo.getEnddate()+"'");
        }else if (StringUtils.isNotBlank(showCouponsVo.getStartdate()) && !StringUtils.isNotBlank(showCouponsVo.getEnddate())){
            sb.append(" and DATE_FORMAT(offdate,'%Y-%m-%d') >='"+showCouponsVo.getStartdate()+"'");
        }else if(!StringUtils.isNotBlank(showCouponsVo.getStartdate()) && StringUtils.isNotBlank(showCouponsVo.getEnddate())){
            sb.append(" and DATE_FORMAT(offdate,'%Y-%m-%d') <='"+showCouponsVo.getEnddate()+"'");
        }

        if (StringUtils.isNotBlank(showCouponsVo.getStartvalue()) && StringUtils.isNotBlank(showCouponsVo.getEndvalue())){
            sb.append(" and value between '"+showCouponsVo.getStartvalue()+"' and '"+showCouponsVo.getEndvalue()+"'");
        }else if (StringUtils.isNotBlank(showCouponsVo.getStartvalue()) && !StringUtils.isNotBlank(showCouponsVo.getEndvalue())){
            sb.append(" and value >='"+showCouponsVo.getStartvalue()+"'");
        }else if(!StringUtils.isNotBlank(showCouponsVo.getStartvalue()) && StringUtils.isNotBlank(showCouponsVo.getEndvalue())){
            sb.append(" and value <='"+showCouponsVo.getStartvalue()+"'");
        }
        Pagination<CroCoupons> croCoupons=croCouponsDao.findPagination(sb.toString(),index,size);
        return croCoupons;
    }



    /*添加优惠券*/
    @Transactional(readOnly = false)
    public boolean addCoupons(CroCoupons coupons){
        try {
            croCouponsDao.save(coupons);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    发放优惠券
    */
    @Transactional(readOnly = false)
    public boolean issueCoupons(CroCoupons croCoupons){
        try{
            croMyqponDao.update(croCoupons);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }



}
